package Yunzai

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"
	"gopkg.in/yaml.v3"
	"strings"
)

func Set_Account(GoBoxPath,BotPath string) {
	var (
		Login_type string
		qq         string
		password   string
	)
	var account Account
	//切换目录
	os.Chdir(GoBoxPath)
	account_path := BotPath + "/config/config/qq.yaml"
	fmt.Println("配置文件:", account_path)
	if _, err := os.Stat(account_path); os.IsNotExist(err) {
		//询问用户是否生
		fmt.Println(Red,"配置文件文件不存在,请完成初始化...", Reset)
		time.Sleep(2 * time.Second)
		return
	}
	data, err := readFile(account_path)
	if err != nil {
		//红色字体
		fmt.Println(Red, "读取配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
	}
	//提示用户输入QQ号码
	fmt.Printf("请输入QQ号码: ")
	// fmt.Scanln(&qq)
	//做校验只允许输入5-12位的数字
	for {
		fmt.Scanln(&qq)
		if len(qq) >= 5 && len(qq) <= 12 && strings.Count(qq, "") == len(qq)+1 && strings.Trim(qq, "0123456789") == "" {
			break
		}
		fmt.Printf("QQ号码输入错误，请重新输入：")
		
	}
	//提示用户输入密码
	fmt.Printf("请输入密码：")
	fmt.Scanln(&password)
	fmt.Printf("登录接口 (1:安卓手机/2:aPad/3:安卓手表/4:MacOS/5:iPad/6:Tim):")
	// fmt.Scanln(&Login_type)
	//做校验只允许输入1-6
	for {
		fmt.Scanln(&Login_type)
		if Login_type == "1" || Login_type == "2" || Login_type == "3" || Login_type == "4" || Login_type == "5" || Login_type == "6" {
			break
		}
		fmt.Printf("登录接口输入错误，请重新输入：")
	}

	//将用户输入的QQ号码和密码写入到结构体中
	account.QQ = qq
	account.Password = password
	account.Login_type = Login_type

	//将结构体转换成yaml格式
	data, err = yaml.Marshal(&account)
	if err != nil {
		log.Println("转换yaml格式失败")
		time.Sleep(2 * time.Second)
		return
	}
	//将yaml格式的数据写入到文件中
	if err := ioutil.WriteFile(account_path, data, 0644); err != nil {
		log.Println("写入文件失败")
		time.Sleep(2 * time.Second)
		return
	}

	//绿色字体输出
	fmt.Println("\033[32m Success：配置文件已写入修改成功,登录选择1,完成初始化\033[0m")
	//查看文件路径
	fmt.Println("文件路径：", account_path)
	time.Sleep(2 * time.Second)
}

